package chapter03.Example03;
class Student03{
    String name;                         //声明姓名属性
    int age;                          //声明年龄属性
    void read(){
        System.out.println("大家好，我是"+name);
    }
}
public class Example03 {
    public static void main(String[] args){
        Student03 stu = new Student03();  //创建Student对象
        stu.name = "张三";               //为stu1对象的name属性赋值
        stu.age = -18;
        stu.read();                     //调用对象方法
    }
}
class Student04 {
    private String name;                         //声明姓名属性
    private int age;                          //声明年龄属性
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if (age<=0){
            System.out.println("您输入的年龄有误！");
        }else {
            this.age = age;
        }
    }
    public void read(){
        System.out.println("大家好，我是"+name+",年龄"+age);
    }

    public static void main(String[] args){
        Student04 stu = new Student04();  //创建Student对象
        stu.setName("张三");               //为stu1对象的name属性赋值
        stu.setAge(-18);                  //为对象的age属性赋值
        stu.read();                     //调用对象方法

    }
}
class Student05{
    public Student05(){
        System.out.println("调用了无参构造方法");
    }

    public static void main(String[] args) {
        System.out.println("声明对象...");
        Student05 stu = null;             //声明对象
        System.out.println("实例化对象...");
        stu = new Student05();            //实例化对象
    }
}
class Student06{
    private String name;
    private int age;
    public Student06 (String n, int a){
        name = n;
        age = a;
    }
    public void read(){
        System.out.println("我是："+name+",年龄"+age);
    }
    public static void main(String[] args) {
        Student06 stu = new Student06("张三", 18);//实例化Student对象
        stu.read();
    }
}
class Student07 {
    private String name;
    private int age;

    public Student07(String n) {
        name = n;
    }

    public Student07(String n, int a) {
        name = n;
        age = a;
    }

    public void read() {
        System.out.println("我是：" + name + ",年龄" + age);
    }
    public static void main(String[] args) {
        Student07 stu1 = new Student07("张三");
        Student07 stu2 = new Student07("张三", 18);//实例化Student对象
        stu1.read();
        stu2.read();
    }
}

class Student08{
    int age;
    public Student08(int n){
        age = n;
    }
    public static void main(String[] args) {
        Student08 stu = new Student08(7);
    }
}
